import tbdp.tas.{Constants, Utils}
import tbdp.tas.model.MinsLine

import scala.collection.mutable.ArrayBuffer

/**
  * Created by xbkaishui on 16/12/10.
  */
object TestWriteTable {

  def main(args: Array[String]) {
    val lines = new ArrayBuffer[MinsLine]()
    val st = new MinsLine
    st.yAgency1BSVolRate = 1
    st.sCode = "ss"
    st.marketType = "11"
    lines.append(st)
    sendOut(lines)
  }


  //输出到 mysql
  def sendOut(lines: Iterable[MinsLine]) = {
    val conn = Utils.getMysqlConn
    val sStock = Constants.mysql_select_stock
    val stockSql = s"INSERT INTO $sStock (SCode,MarketType,DynamicVal,TopData1,TopData3,TopData5,AllSco,Agency1BSVolRate,Agency3BSVolRate,Agency5BSVolRate,Agency10BSVolRate,Agency20BSVolRate,YAgency1BSVolRate) " +
      s"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE SCode=? ,MarketType=?, DynamicVal=? ,TopData1=? , TopData3=?,TopData5=?, AllSco=? , Agency1BSVolRate=?,Agency3BSVolRate=?," +
      s"Agency5BSVolRate=?,Agency10BSVolRate=?,Agency20BSVolRate=?,YAgency1BSVolRate=? ;"
    val stmt = conn.prepareStatement(stockSql)
    var idx = 0
    lines.foreach {
      st => {
        //        println(st.sCode +":"+st.marketType +":\t"+st.agency10BSVolRate)
        idx = idx + 1
        //prepare
        val numList = List(0, 13)
        for (inc <- numList) {
          stmt.setString(1 + inc, st.sCode)
          stmt.setInt(2 + inc, st.marketType.toInt)
          stmt.setInt(3 + inc, st.dynamicVal)
          stmt.setDouble(4 + inc, Utils.formatNumber(st.topData1, 3))
          stmt.setDouble(5 + inc, Utils.formatNumber(st.topData3, 3))
          stmt.setDouble(6 + inc, Utils.formatNumber(st.topData5, 3))
          stmt.setInt(7 + inc, st.allSco)
          stmt.setDouble(8 + inc, Utils.formatNumber(st.agency1BSVolRate, 2))
          stmt.setDouble(9 + inc, Utils.formatNumber(st.agency3BSVolRate, 2))
          stmt.setDouble(10 + inc, Utils.formatNumber(st.agency5BSVolRate, 2))
          stmt.setDouble(11 + inc, Utils.formatNumber(st.agency10BSVolRate, 2))
          stmt.setDouble(12 + inc, Utils.formatNumber(st.agency20BSVolRate, 2))
          stmt.setDouble(13 + inc, Utils.formatNumber(st.yAgency1BSVolRate, 2))
        }
                println("stmt sql" + stmt.toString)
        //add batch
        stmt.addBatch()
        //batch execute
        if (idx == 1000) {
          //batch send out
          stmt.executeBatch()
          idx = 0
        }
      }
    }
    if (idx > 0) {
      stmt.executeBatch()
    }
    stmt.close()
    conn.close()
  }

}
